/* 
 * Copyright 2013 Sébastien Mouquet.
 * 
 * This file is part of ECovoiturage.
 * 
 * ECovoiturage is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ECovoiturage is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ECovoiturage. If not, see http://www.gnu.org/licenses/.
 */
package com.smouquet.ecovoiturage;

import com.google.inject.AbstractModule;
import com.google.inject.name.Names;
import com.google.inject.persist.jpa.JpaPersistModule;
import com.smouquet.ecovoiturage.core.bean.Config;
import com.smouquet.ecovoiturage.core.dao.EntrepriseDAO;
import com.smouquet.ecovoiturage.core.dao.LieuDAO;
import com.smouquet.ecovoiturage.core.dao.TrajetDAO;
import com.smouquet.ecovoiturage.core.dao.UtilisateurDAO;
import com.smouquet.ecovoiturage.core.dao.impl.EntrepriseDAOImpl;
import com.smouquet.ecovoiturage.core.dao.impl.LieuDAOImpl;
import com.smouquet.ecovoiturage.core.dao.impl.TrajetDAOImpl;
import com.smouquet.ecovoiturage.core.dao.impl.UtilisateurDAOImpl;
import com.smouquet.ecovoiturage.core.dbmaintenance.ScriptRepository;
import com.smouquet.ecovoiturage.core.dbmaintenance.script.derby.DerbyScriptRepository;
import com.smouquet.ecovoiturage.core.dbmaintenance.script.postgres.PostgresScriptRepository;
import com.smouquet.ecovoiturage.core.service.ConfigService;
import com.smouquet.ecovoiturage.core.service.DbMaintenanceService;
import com.smouquet.ecovoiturage.core.service.EntrepriseService;
import com.smouquet.ecovoiturage.core.service.LieuService;
import com.smouquet.ecovoiturage.core.service.MailService;
import com.smouquet.ecovoiturage.core.service.TrajetService;
import com.smouquet.ecovoiturage.core.service.UtilisateurService;
import com.smouquet.ecovoiturage.core.service.impl.ConfigServiceImpl;
import com.smouquet.ecovoiturage.core.service.impl.DbMaintenanceServiceImpl;
import com.smouquet.ecovoiturage.core.service.impl.EntrepriseServiceImpl;
import com.smouquet.ecovoiturage.core.service.impl.LieuServiceImpl;
import com.smouquet.ecovoiturage.core.service.impl.MailServiceImpl;
import com.smouquet.ecovoiturage.core.service.impl.TrajetServiceImpl;
import com.smouquet.ecovoiturage.core.service.impl.UtilisateurServiceImpl;
import java.util.Properties;

/**
 *
 * @author sebastien.mouquet
 */
public class ApplicationModule extends AbstractModule
{
    private Config config;
    
    public ApplicationModule() {}
    
    public ApplicationModule(Config config)
    {
        this.config = config;
    }

    @Override
    protected void configure()
    {
        // dbmaintenance
        bind(ScriptRepository.class).annotatedWith(Names.named("derby")).to(DerbyScriptRepository.class);
        bind(ScriptRepository.class).annotatedWith(Names.named("postgres")).to(PostgresScriptRepository.class);
        
        // service
        bind(ConfigService.class).to(ConfigServiceImpl.class);
        bind(DbMaintenanceService.class).to(DbMaintenanceServiceImpl.class);
        
        
        if(config != null)
        {
            // JPA module
            Properties properties = new Properties();
            properties.setProperty(ConfigService.PROPERTY_JDBC_DRIVER, config.getJdbcDriver());
            properties.setProperty(ConfigService.PROPERTY_JDBC_URL, config.getJdbcUrl());
            properties.setProperty(ConfigService.PROPERTY_JDBC_USER, config.getJdbcUser());
            properties.setProperty(ConfigService.PROPERTY_JDBC_PASSWORD, config.getJdbcPassword());
            
            JpaPersistModule jpaModule = new JpaPersistModule("ECovoituragePU");
            jpaModule.properties(properties);
            
            install(jpaModule);
            
            // Instance bindings
            bind(String.class).annotatedWith(Names.named(ConfigService.PROPERTY_MAIL_SMTP_HOST)).toInstance(config.getMailSmtpHost());
            
            // DAO bindings
            bind(EntrepriseDAO.class).to(EntrepriseDAOImpl.class);
            bind(LieuDAO.class).to(LieuDAOImpl.class);
            bind(TrajetDAO.class).to(TrajetDAOImpl.class);
            bind(UtilisateurDAO.class).to(UtilisateurDAOImpl.class);

            // Service bindings
            bind(EntrepriseService.class).to(EntrepriseServiceImpl.class);
            bind(LieuService.class).to(LieuServiceImpl.class);
            bind(MailService.class).to(MailServiceImpl.class);
            bind(TrajetService.class).to(TrajetServiceImpl.class);
            bind(UtilisateurService.class).to(UtilisateurServiceImpl.class);
        }
    }
    
}
